<?php
function getPaginationString($page = 1, $totalitems=100, $limit = 8, $adjacents = 1, $targetpage = "/", $pagestring = "/index.php/expenses/page=",$margin=5,$padding=5)
{		
	//defaults
	if(!$adjacents) $adjacents = 1;
	if(!$limit) $limit = 15;
	if(!$page) $page = 1;
	if(!$targetpage) $targetpage = "/";
	
	//other vars
	$prev = $page - 1;									//previous page is page - 1
	$next = $page + 1;									//next page is page + 1
	$lastpage = ceil($totalitems / $limit);				//lastpage is = total items / items per page, rounded up.
	$lpm1 = $lastpage - 1;								//last page minus 1
	
	/* 
		Now we apply our rules and draw the pagination object. 
		We're actually saving the code to a variable in case we want to draw it more than once.
	*/
	$pagination = "";
	if($lastpage > 1)
	{	
		$pagination .= "<div class=\"pagination\"";
		if($margin || $padding)
		{
			$pagination .= " style=\"";
			if($margin)
				$pagination .= "margin: $margin;";
			if($padding)
				$pagination .= "padding: $padding;";
			$pagination .= "\"";
		}
		$pagination .= ">";

		//previous button
		if ($page > 1) 
			$pagination .= "<a class=\"pagingink\" href=\"$targetpage$pagestring$prev\">&#171; prev</a>";
		else
			$pagination .= "<span class=\"disabled\">&#171; prev</span>";	
		
		//pages	
		if ($lastpage < 7 + ($adjacents * 2))	//not enough pages to bother breaking it up
		{	
			for ($counter = 1; $counter <= $lastpage; $counter++)
			{
				if ($counter == $page)
					$pagination .= "<span class=\"current\">$counter</span>";
				else
					$pagination .= "<a class=\"pagingink\" href=\"" . $targetpage . $pagestring . $counter . "\">$counter</a>";					
			}
		}
		elseif($lastpage >= 7 + ($adjacents * 2))	//enough pages to hide some
		{
			//close to beginning; only hide later pages
			if($page < 1 + ($adjacents * 3))		
			{
				for ($counter = 1; $counter < 4 + ($adjacents * 2); $counter++)
				{
					if ($counter == $page)
						$pagination .= "<span class=\"current\">$counter</span>";
					else
						$pagination .= "<a class=\"pagingink\" href=\"" . $targetpage . $pagestring . $counter . "\">$counter</a>";					
				}
				$pagination .= "<span class=\"elipses\">...</span>";
				$pagination .= "<a class=\"pagingink\" href=\"" . $targetpage . $pagestring . $lpm1 . "\">$lpm1</a>";
				$pagination .= "<a class=\"pagingink\" href=\"" . $targetpage . $pagestring . $lastpage . "\">$lastpage</a>";		
			}
			//in middle; hide some front and some back
			elseif($lastpage - ($adjacents * 2) > $page && $page > ($adjacents * 2))
			{
				$pagination .= "<a class=\"pagingink\" href=\"" . $targetpage . $pagestring . "1\">1</a>";
				$pagination .= "<a class=\"pagingink\" href=\"" . $targetpage . $pagestring . "2\">2</a>";
				$pagination .= "<span class=\"elipses\">...</span>";
				for ($counter = $page - $adjacents; $counter <= $page + $adjacents; $counter++)
				{
					if ($counter == $page)
						$pagination .= "<span class=\"current\">$counter</span>";
					else
						$pagination .= "<a class=\"pagingink\" href=\"" . $targetpage . $pagestring . $counter . "\">$counter</a>";					
				}
				$pagination .= "...";
				$pagination .= "<a class=\"pagingink\" href=\"" . $targetpage . $pagestring . $lpm1 . "\">$lpm1</a>";
				$pagination .= "<a class=\"pagingink\" href=\"" . $targetpage . $pagestring . $lastpage . "\">$lastpage</a>";		
			}
			//close to end; only hide early pages
			else
			{
				$pagination .= "<a class=\"pagingink\" href=\"" . $targetpage . $pagestring . "1\">1</a>";
				$pagination .= "<a class=\"pagingink\" href=\"" . $targetpage . $pagestring . "2\">2</a>";
				$pagination .= "<span class=\"elipses\">...</span>";
				for ($counter = $lastpage - (1 + ($adjacents * 3)); $counter <= $lastpage; $counter++)
				{
					if ($counter == $page)
						$pagination .= "<span class=\"current\">$counter</span>";
					else
						$pagination .= "<a class=\"pagingink\" href=\"" . $targetpage . $pagestring . $counter . "\">$counter</a>";					
				}
			}
		}
		
		//next button
		if ($page < $counter - 1) 
			$pagination .= "<a class=\"pagingink\" href=\"" . $targetpage . $pagestring . $next . "\">next &#187;</a>";
		else
			$pagination .= "<span class=\"disabled\">next &#187;</span>";
		$pagination .= "</div>\n";
	}
	
	return $pagination;

}
?>

 <table class="display editInPlace" id="expensetable" border="0" cellpadding="0" cellspacing="1">
                <thead>
                <tr>
                    <th class="checkbox  sorting_disabled"><input id="expenses_selectall_checkbox"
                                                                  class="selectall_checkbox" type="checkbox"></th>
                    <th class="created js_table_sort sorting">Date</th>
                    <th class="merchant js_table_sort sorting">Merchant</th>
                    <th class="billable js_table_sort sorting">Billable</th>
                    <th class="onight js_table_sort sorting">Claim Customer</th>
                    <th class="amount js_table_sort sorting">Total</th>
                    <th class="currency js_table_sort sorting hidden">ExchangeRate</th>
                    <th class="category js_table_sort sorting">Category</th>
                    <th class="receiptcolumn  sorting_disabled">Receipt</th>
                    
<!--                    <th class="tag js_table_sort sorting">Description</th>-->
<!--                    <th class="comment js_table_sort sorting">Comment</th>-->
                    
                </tr>
                </thead>
                <tbody>
                <?php $i=0;?>
                <?php foreach ($rows as $row): ?>
                <?php if($i%2):?>
                 <tr id="expensetable_<?php echo $row['Id'];?>_<?php echo $row['Row']?>_<?php echo $totalItem;?>" class="odd hoverable">
                <?php else:?>
                 <tr id="expensetable_<?php echo $row['Id'];?>_<?php echo $row['Row']?>_<?php echo $totalItem;?>" class="even hoverable">
                <?php endif;?>
                    <td class="checkbox">
                        <div class="edit-wrapper Edit"></div>
                        <div class="edit EXPENSIFYEDIT_FULLEDIT js_editTransaction Edit EXPENSIFY_REPORTED"><a
                                class="megaEdit"> Edit</a></div>
                        <input class="expensetable_checkbox" type="checkbox" value="<?php echo $row['Id'];?>">
                    </td>
                    <td class="datecolumn editable js_editDate"><?php echo date('Y-m-d',strtotime($row['Date']));?></td>
                    <td class="merchantcolumn editable js_editDate"><?php echo $row['Merchant'];?></td>
                    <?php if($row['Billable']):?>
                    	<td class="billablecolumn editable js_editMerchant"><input type="checkbox" checked="checked" /></td>
                    <?php else:?>
                       <td class="billablecolumn editable js_editMerchant"><input type="checkbox"/></td>
                    <?php endif;?>
                    <?php if($row['ClaimCustomer']):?>
                    	<td class="customerclaimcolumn editable js_editMerchant"><input type="checkbox" checked="checked" /></td>
                    <?php else:?>
                    	<td class="customerclaimcolumn editable js_editMerchant"><input type="checkbox"/></td>
                    <?php endif;?>
                    <td class="amountcolumn editable js_editAmount">
                    	<?php echo number_format( $row['Total'],2);?>
                         <?php foreach ($currencies as $currency):?>
                         <?php if($currency['Id']==$row['CurrencyId']) echo $currency['CurrencyName'];?>
                         <?php endforeach;?>
                    </td>
                    <td class="exchangeratecolumn editable js_editAmount hidden"><?php echo $row['ExchangeRate'];?></td>
                    <td class="categorycolumn editable js_editCategory"><span class="implicitcategorization"><?php echo $row['Category'];?></span>
                    </td>
                    <?php if($row['ReceiptId'] == null || $row['ReceiptId'] == 0):?>
                    <td id="<?php echo $row['ReceiptId'];?>" class="receiptcolumn"><span class="addReceiptIcons">&nbsp;</span></td>
                    <?php else:?>
                    <td id="<?php echo $row['ReceiptId'];?>" class="receiptcolumn"><span class="receiptIcons">&nbsp;</span></td>
                    <?php endif;?>
                    <!--
                    <td class="desctipyioncolumn js_table_sort editable js_editDescription"><?php //echo $row['Description'];?></td>
                    <td class="commentcolumn editable js_editComment"><?php //echo $row['Comment'];?></td>
                --></tr>
                <?php $i++;?>
                <?php endforeach;?>
                <tr>
                	<td colspan="9">
                	<?php echo getPaginationString($page,$totalItem,$limit,1,Yii::app()->params['domain'].'/','expenses/index/page/');?>          
                	
                	</td>
                </tr>
                </tbody>
            </table>
  <div class="clear"></div>
